package com.gitee.wsl.common.ui.ext.path

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Path
import com.gitee.wsl.unit.angle.AngleUnit
import com.gitee.wsl.unit.angle.ext.calcXY
import com.gitee.wsl.unit.angle.ext.plus

internal const val DegreesFullCircle = 360.0f

fun Path.slice(size: Size,
               startAngle: AngleUnit,
               angle: AngleUnit,
               innerRadius: Float = 0.5f,
               outerRadius: Float = 1.0f){
    val widthHalf = size.width / 2f
    slice(
        startAngle = startAngle,
        angle = angle,
        radius = widthHalf * outerRadius,
        holeRadius = widthHalf * innerRadius,
        center = Offset(widthHalf, widthHalf),
        widthHalf = widthHalf
    )
}



fun Path.slice(startAngle: AngleUnit,
               angle: AngleUnit,
               radius: Float,
               holeRadius: Float,
               center : Offset,
               widthHalf:Float,
){
    if (angle == AngleUnit.TWO_PI && holeRadius != 0f) {
        // Outer circle
        addArc(
            Rect(
                widthHalf  - radius,
                widthHalf  - radius,
                widthHalf  + radius,
                widthHalf  + radius
            ),
            startAngle.degreesF,
            angle.degreesF
        )
        // Inner circle
        addArc(
            Rect(
                widthHalf  - holeRadius,
                widthHalf  - holeRadius,
                widthHalf  + holeRadius,
                widthHalf  + holeRadius
            ),
            startAngle.degreesF,
            -angle.degreesF
        )
    } else {

        // First line segment from start point to first outer corner
        //moveTo(center + startAngle.polarToCartesian(holeRadius))
        //lineTo(center + startAngle.polarToCartesian(radius))

        moveTo(startAngle.calcXY(center,holeRadius))
        lineTo(startAngle.calcXY(center,radius))

        // Outer arc
        addArc(
            Rect(
                widthHalf  - radius,
                widthHalf  - radius,
                widthHalf  + radius,
                widthHalf  + radius
            ),
            startAngle.degreesF,
            angle.degreesF
        )
        val end =  startAngle + angle
        // Line from second outer corner to inner corner or center
        //lineTo(center + end.polarToCartesian(holeRadius))
        lineTo(end.calcXY(center ,holeRadius))

        if (holeRadius != 0f) {
            // Inner arc
            arcTo(
                Rect(
                    widthHalf  - holeRadius,
                    widthHalf  - holeRadius,
                    widthHalf + holeRadius,
                    widthHalf + holeRadius
                ),
                end.degreesF,
                -angle.degreesF,
                false,
            )
        }
    }

}

